const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    key: '',
    houseid: null,
    chukuList: [],
    more: true,
    moveData1: null,
    moveData2: null,
    inputShowed: false,
    inputVal: "",
    inputVal2: "搜索",
    isHideLoadMore: true,
    masking: false,
    houseid: null,
    houseList: [],
    image: '../images/screen.png',
  },
  changeText: function (e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
  },
  clickScreen: function () {
    if (this.data.masking) {
      this.setData({
        masking: false,
        image: '../images/screen.png'
      })
    } else {
      this.setData({
        masking: true,
        image: '../images/screen-active.png'
      })
    }
  },
  sltScn: function (e) {
    console.log(e)
    var id = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : null
    console.log(id)
    this.setData({
      page: 1,
      houseid: id
    })
    this.getList()
  },
  toAddRuku: function () {
    wx.navigateTo({
      url: './newRuku/newRuku'
    })
  }, 
  moreClick: function () {
    console.log(this.data.more)
    var animation1 = wx.createAnimation({
      duration: 3000,
      delay: 0,
      timingFunction: "ease",
    })
    var animation2 = wx.createAnimation({
      duration: 3000,
      delay: 0,
      timingFunction: "ease",
    })
    if (this.data.more) {
      animation1.translate(-55, 0).step({ duration: 1000 })
      animation2.translate(0, -55).step({ duration: 1000 })
    } else {
      animation1.translate(0, 0).step({ duration: 1000 })
      animation2.translate(0, 0).step({ duration: 1000 })
    }
    this.setData({
      more: !this.data.more,
      moveData1: animation1.export(),
      moveData2: animation2.export()
      // scaleData: animation.export()
    })
  },
  search: function () {
    this.setData({
      page: 1
    })
    this.getList()
  },
  open: function (e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showActionSheet({
      itemList: ['预览', '删除'],
      success: function (res) {
        console.log(res)
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            wx.navigateTo({
              url: './newChuku/newChuku?id=' + id
            })
          }  else if (res.tapIndex == 1) {
            wx.showModal({
              title: '删除产品',
              content: '你确定删除该产品吗？',
              confirmText: "删除",
              cancelText: "取消",
              success: function (res) {
                console.log(res);
                if (res.confirm) {
                  console.log('删除')
                  app.req({
                    url: "HouseStore/OutStore/" + id,
                    method: "DELETE",
                    success: function (res) {
                      if (res.data.Code) {
                        wx.showToast({
                          title: '删除成功',
                          icon: 'success',
                          duration: 1500
                        });
                        wx.startPullDownRefresh()
                      } else {
                        wx.showToast({
                          title: '删除失败',
                          duration: 1500
                        });
                      }
                    }
                  })
                } else {
                  console.log('取消')
                }
              }
            });
          }
        }
      }
    });
  },
  getList: function () {
    var that = this
    app.req({
      url: 'House/OutStore',
      data: {
        pageindex: this.data.page,
        pagesize: 15,
        key: this.data.key
      },
      success: function (res) {
        if (res.data.result.length > 0) {
          var arr = res.data.result
          for (let i = 0; i < arr.length; i++) {
            arr[i].date = []
            var arr1 = app.dateFormat(arr[i].lastUpdateTime)
            // arr[i].date.concat(arr1)
            arr[i].date.push(arr1[0])
            arr[i].date.push(arr1[1])
            console.log(arr[i].date)
          }
          if (that.data.page == 1) {
            that.setData({
              chukuList: arr
            })
          } else if (that.data.page > 1) {
            that.setData({
              chukuList: that.data.chukuList.concat(arr)
            })
          }
        } else {
          that.setData({
            page: that.data.page - 1
          })
        }
        setTimeout(function () {
          wx.stopPullDownRefresh()
        }, 1000)

      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this
    // app.req({
    //   url: "Configure/Houses",
    //   success: function (res) {
    //     console.log(res.data.Result)
    //     that.setData({
    //       houseList: res.data.Result
    //     })
    //   }
    // })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.startPullDownRefresh({

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      key: ''
    })
    this.getList()

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多')
    this.setData({
      isHideLoadMore: false,
    })
    this.setData({
      page: this.data.page + 1
    })
    this.getList()
    setTimeout(() => {
      this.setData({
        isHideLoadMore: true,
      })
    }, 1000)

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})